//
//  ZXLoginAPI.m
//  ZXLoginKit
//
//  Created by 海参 on 2022/10/9.
//

#import "ZXLoginAPI.h"
#import <objc/runtime.h>
#import <objc/message.h>

@implementation ZXLoginAPI

+ (instancetype)sharedInstance
{
    static dispatch_once_t once;
    static id obj = nil;
    dispatch_once(&once, ^{
        obj = [[self alloc]init];
    });
    return obj;
}

- (void)initWithUMAppKey:(NSString *)appKey
{
    // 初始化基础组件库
//    [UMConfigure initWithAppkey:appKey channel:nil];
    Class cls = objc_getClass("UMConfigure");//NSClassFromString(@"UMConfigure");
    void (*umConfigure_msgSend)(id,SEL,NSString *,NSString *) = (void (*)(id, SEL,NSString *,NSString *))objc_msgSend;
    umConfigure_msgSend(cls,sel_registerName("initWithAppkey:channel:"),appKey,nil);
}

#pragma mark - UM一键登录

//    -(void) setVerifySDKInfo:(NSString*_Nonnull)info complete:(void(^_Nullable)(NSDictionary*_Nonnull resultDic))complete;
- (void)setOneKeyLoginAuth:(NSString *)auth{
    Class cls = objc_getClass("UMCommonHandler");
    void (*method_msgSend)(id,SEL,NSString *,void(^_Nullable block)(NSDictionary * _Nonnull resultDic)) = (void (*)(id,SEL,NSString *,void(^_Nullable block)(NSDictionary * _Nonnull resultDic)))objc_msgSend;
    method_msgSend(cls,sel_registerName("setVerifySDKInfo:complete:"),auth,^(NSDictionary*_Nonnull resultDic){
        NSString *resultCode = [resultDic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"600000"]) {
            
            void (*method_msgSend2)(id,SEL,NSTimeInterval,void(^_Nullable block)(NSDictionary * _Nonnull resultDic)) = (void (*)(id,SEL,NSTimeInterval,void(^_Nullable block)(NSDictionary * _Nonnull resultDic)))objc_msgSend;
            method_msgSend2(cls,sel_registerName("accelerateLoginPageWithTimeout:complete:"),3,^(NSDictionary*_Nonnull resultDic){
                
            });
        }
    });
}

//    +(void)checkEnvAvailableWithAuthType:(UMPNSAuthType)authType complete:(void(^_Nullable)(NSDictionary*_Nullable resultDic))complete;
- (void)checkOneKeyLoginEnvAvailableWithComplete:(void (^)(BOOL enable))complete{
    Class cls = objc_getClass("UMCommonHandler");
    void (*method_msgSend)(id,SEL,NSInteger,void(^_Nullable block)(NSDictionary * _Nonnull resultDic)) = (void (*)(id,SEL,NSInteger,void(^_Nullable block)(NSDictionary * _Nonnull resultDic)))objc_msgSend;
    method_msgSend(cls,sel_registerName("checkEnvAvailableWithAuthType:complete:"),2,^(NSDictionary*_Nonnull resultDic){
        NSString *resultCode = [resultDic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"600000"]) {
            if (complete) {
                complete(YES);
            }
        }else{
            if (complete) {
                complete(NO);
            }
        }
    });
}


//[UMCommonHandler getLoginTokenWithTimeout:3 controller:self model:nil complete:^(NSDictionary * _Nonnull resultDic) {
//
//}];
- (void)chsOneKeyLoginWithController:(UIViewController *_Nonnull)controller
                               model:(id)model
                            complete:(void (^_Nullable)(NSDictionary * _Nonnull resultDic))complete
{
    Class cls = objc_getClass("UMCommonHandler");
    void (*method_msgSend)(id,SEL,NSTimeInterval,UIViewController *,id,void(^_Nullable block)(NSDictionary * _Nonnull resultDic)) = (void (*)(id,SEL,NSTimeInterval,UIViewController *,id,void(^_Nullable block)(NSDictionary * _Nonnull resultDic)))objc_msgSend;
    method_msgSend(cls,sel_registerName("getLoginTokenWithTimeout:controller:model:complete:"),3,controller,model,^(NSDictionary*_Nonnull resultDic){
        NSString *resultCode = [resultDic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"600000"]) {
            NSString *token = [resultDic objectForKey:@"token"];
            NSString *verifyId = ((NSString * (*)(id,SEL))objc_msgSend)(objc_getClass("UMCommonHandler"),@selector(getVerifyId));
            NSDictionary *param = @{@"accessToken":token,@"verifyId":verifyId};
            NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:resultDic];
            [mDic addEntriesFromDictionary:param];
            complete([mDic copy]);
        }
        else if ([resultCode isEqualToString:@"700000"]) {
            void (*method_msgSend2)(id,SEL,BOOL,void (^_Nullable)(void)) = (void (*)(id,SEL,BOOL,void (^_Nullable)(void)))objc_msgSend;
            method_msgSend2(cls,sel_registerName("cancelLoginVCAnimated:complete:"),YES,nil);
            if (complete) {
                complete(resultDic);
            }
        }
        else{
            if (complete) {
                complete(resultDic);
            }
        }
    });
}

@end
